package com.probox.common.core.utils;

import cn.hutool.core.util.ObjectUtil;
import com.probox.common.core.constant.CacheConstants;
import com.probox.common.core.text.Convert;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 权限获取工具类
 *
 * @author usoft
 */
public class SecurityUtils {

    public static final String REGEX_PASSWORD_STRONG = "^(?![0-9]+$)(?![^0-9]+$)(?![a-zA-Z]+$)(?![^a-zA-Z]+$)(?![a-zA-Z0-9]+$)[a-zA-Z0-9\\S]{6,}$";


    /**
     * @param rex 正则校验规则
     * @param str 要校验的字符串
     * @return 返回校验结果，若满足校验规则，则返回true，否则返回false
     */
    public static Boolean match(String rex, String str) {
        // TODO Auto-generated method stub
        Pattern pattern = Pattern.compile(rex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    /**
     * 获取后端用户
     */
    public static String getUsername() {
        String username = ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USERNAME);
        if (ObjectUtil.isEmpty(username)) return "";
        return ServletUtils.urlDecode(username);
    }

    /**
     * 获取后端用户ID
     */
    public static Long getUserId() {
        return Convert.toLong(ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USER_ID));
    }

    public static  Long getStoreId(){
        return Convert.toLong(ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_STORE_ID));
    }


    /**
     * 获取医生账号
     *
     * @return
     */
    public static Long getDoctorId() {
        String doctorId = ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_DOCTOR_ID);
        if (doctorId == null) {
            return null;
        }
        return Convert.toLong(doctorId);
    }

    /**
     * 获取后台用户的orgid
     *
     * @return
     */
    public static Long getOrgId() {
        String header = ServletUtils.getRequest().getHeader(CacheConstants.ORGAN_ID);
        if (ObjectUtil.isEmpty(header)) return 0l;
        return Convert.toLong(header);
    }

    /**
     * 获取后端用户平台ID
     */
    public static Long getPlatformId() {
        return 1L;
        //return Convert.toLong(ServletUtils.urlDecode(ServletUtils.getRequest().getHeader(CacheConstants.PLATFORM_HEADER)));
    }

    /**
     * 获得后端用户供应商ID
     */
    public static Long getSupplierId() {
        String header = ServletUtils.getRequest().getHeader(CacheConstants.SUPPLIER_UID);
        if (header == null) {
            return 0L;
        }
        return Convert.toLong(ServletUtils.urlDecode(header));
    }

    /**
     * 获得前端客户的ID
     *
     * @return
     */
    public static Long getCustomerUid() {
        String customerUid = ServletUtils.getRequest().getHeader(CacheConstants.CUSTOMER_ID);
        if (StringUtils.isNotEmpty(customerUid)) {
            return Convert.toLong(customerUid);
        }
        return null;
    }

    /**
     * 获取前端客户的账号ID
     *
     * @return
     */
    public static Long getCustomerAccountUid() {
        String customerAcountUid = ServletUtils.getRequest().getHeader(CacheConstants.CUSTOMER_ACCOUNT_ID);
        if (StringUtils.isNotEmpty(customerAcountUid)) {
            return Convert.toLong(customerAcountUid);
        }
        return null;
    }

    public static String getCustomerAccountStatus() {
        String customerAcountStatus = ServletUtils.getRequest().getHeader(CacheConstants.CUSTOMER_ACCOUNT_STATUS);
        return customerAcountStatus;
    }

    /**
     * 获取前端客户的平台ID
     *
     * @return
     */
    public static Long getCustomerPlatformUid() {
        return 1L;
    }

    /**
     * 获取前端客户的用户名称
     *
     * @return
     */
    public static String getCustomerUserName() {
        String userName = ServletUtils.getRequest().getHeader(CacheConstants.CUSTOMER_USERNAME);
        if (!ObjectUtil.isEmpty(userName)) {
            try {
                return URLDecoder.decode(userName, "UTF-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 获取客户的电话号码
     *
     * @return
     */
    public static String getCustomerPhone() {
        return ServletUtils.getRequest().getHeader(CacheConstants.CUSTOMER_PHONE);
    }

    /**
     * 获取前端客户的IP地址
     *
     * @return
     */
    public static String getCustomerClientIp() {
        return ServletUtils.getRequest().getHeader(CacheConstants.CUSTOMER_IP);
    }

    /**
     * 获得前端用户的open id
     *
     * @return
     */
    public static String getCustomerOpenId() {
        return ServletUtils.getRequest().getHeader(CacheConstants.CUSTOMER_OPEN_ID);
    }

    /**
     * 前端客户的 APPID
     *
     * @return
     */
    public static String getCustomerAppId() {
        return "wx865be3ae67da2c87";
        //return ServletUtils.getRequest().getHeader(CacheConstants.CUSTOMER_APP_ID);
    }

    /**
     * 前端客户的商户ID
     *
     * @return
     */
    public static String getCustomerMchId() {
        return "1725766500";
        //return ServletUtils.getRequest().getHeader(CacheConstants.CUSTOMER_MCH_ID);
    }

    /**
     * 获得前端客户的secret key
     *
     * @return
     */
    public static String getCustomerSecretKey() {
        return ServletUtils.getRequest().getHeader(CacheConstants.CUSTOMER_SECRET_KEY);
    }

    /**
     * 获得前端用户的source port
     *
     * @return
     */
    public static String getCustomerSourcePort() {
        return ServletUtils.getRequest().getHeader(CacheConstants.CUSTOMER_PORT);
    }


    /**
     * =
     * 获取请求token
     */
    public static String getToken() {
        return getToken(ServletUtils.getRequest());
    }

    /**
     * 根据request获取请求token
     */
    public static String getToken(HttpServletRequest request) {
        String token = ServletUtils.getRequest().getHeader(CacheConstants.HEADER);
        if (StringUtils.isNotEmpty(token) && token.startsWith(CacheConstants.TOKEN_PREFIX)) {
            token = token.replace(CacheConstants.TOKEN_PREFIX, "");
        }
        return token;
    }

    /**
     * 是否为管理员
     *
     * @param userId 用户ID
     * @return 结果
     */
    public static boolean isAdmin(Long userId) {
        return userId != null && 1L == userId;
    }

    /**
     * 生成BCryptPasswordEncoder密码
     *
     * @param password 密码
     * @return 加密字符串
     */
    public static String encryptPassword(String password) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(password);
    }

    /**
     * 判断密码是否相同
     *
     * @param rawPassword     真实密码
     * @param encodedPassword 加密后字符
     * @return 结果
     */
    public static boolean matchesPassword(String rawPassword, String encodedPassword) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }
}
